package com.kim.rssreader.view;

import android.app.Activity;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.FloatMath;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

import com.kim.rssreader.utils.ImageScaleUtil;

public class ScaleDragableTouchListener implements OnTouchListener {

	private ImageViewForDialog scaleDragableImageView;

	private Matrix matrix = new Matrix();
	private Matrix savedMatrix = new Matrix();
	private Matrix initMatrix = new Matrix();
	
	float[] initvalues = new float[9];


	static final int NONE = 0;
	static final int DRAG = 1;
	static final int ZOOM = 2;
	int mode = NONE;

	// Remember some things for zooming
	PointF start = new PointF();
	PointF mid = new PointF();
	float oldDist = 1f;

	Rect rect;
	float imgWidthDefault = 0;
	float imgHeightDefault = 0;

	public ScaleDragableTouchListener(Activity context,
			ImageViewForDialog scaleDragableImageView, Drawable drawable) {
		this.scaleDragableImageView = scaleDragableImageView;
		scaleDragableImageView.setOnTouchListener(this);
		scaleDragableImageView.setLongClickable(true);

		// 扩展到适屏大小
		ImageScaleUtil.imageScale(context, scaleDragableImageView, drawable,
				null, null);
		rect = drawable.getBounds();

		initMatrix.set(scaleDragableImageView.getImageMatrix());
		initMatrix.getValues(initvalues);		
	}

	private float spacing(MotionEvent event) {
		try {
			float x = event.getX(0) - event.getX(1);
			float y = event.getY(0) - event.getY(1);
			return FloatMath.sqrt(x * x + y * y);
		} catch (Exception e) {
			Log.d("ScaleDragableTouchListener",
					"java.lang.IllegalArgumentException:pointerIndex out of range");
		}
		return 0;
	}

	private void midPoint(PointF point, MotionEvent event) {
		float x = event.getX(0) + event.getX(1);
		float y = event.getY(0) + event.getY(1);
		point.set(x / 2, y / 2);
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			matrix.set(scaleDragableImageView.getImageMatrix());
			savedMatrix.set(matrix);
			start.set(event.getX(), event.getY());
			mode = DRAG;
			scaleDragableImageView.setOnlyImageRespond(true);
			break;
		case /* MotionEvent.ACTION_POINTER_DOWN */261: // 多点触控
			oldDist = this.spacing(event);
			if (oldDist > 10f) {
				savedMatrix.set(matrix);
				midPoint(mid, event);
				mode = ZOOM;
			}
			break;
		case /* MotionEvent.ACTION_POINTER_UP */262:
			mode = NONE;
			break;
		case MotionEvent.ACTION_MOVE:
			if (mode == DRAG) { // 此实现图片的拖动功能...

				matrix.set(savedMatrix);
				
				// imageView在窗口中的坐标位置
				Rect localrect = new Rect();
				scaleDragableImageView.getGlobalVisibleRect(localrect);

				float[] values = new float[9];
				matrix.getValues(values);

				// imageview中实际图像在屏幕中的位置
				Rect imageState = new Rect();
				imageState.left = (int)values[2] + localrect.left;
				imageState.top = (int)values[5] + localrect.top;
				imageState.right = (int)(imageState.left + rect.width() * values[0]);
				imageState.bottom = (int)(imageState.top + rect.height() * values[0]);
		
				int movelength_h = (int)(event.getX() - start.x);
				int movelength_v = (int)(event.getY() - start.y);
						
				int movelength_h_fit = ImageScaleUtil.imageFitHMove(imageState, localrect,movelength_h);
				
				if((movelength_h!=movelength_h_fit)&&(Math.abs(movelength_h)>Math.abs(movelength_v)))
				{
					scaleDragableImageView.setOnlyImageRespond(false);
				}
				
				int movelength_v_fit = ImageScaleUtil.imageFitVMove(imageState, localrect,movelength_v);
				
				if(initvalues[0] >= values[0])
				{
					movelength_v_fit = 0;
				}
								
				if (!scaleDragableImageView.getOnlyImageRespond()) {
					return true;
				}
		
			matrix.postTranslate(movelength_h_fit, movelength_v_fit);
				
			} else if (mode == ZOOM) {// 此实现图片的缩放功能...
				float newDist = spacing(event);
				if (newDist > 10) {

					matrix.set(savedMatrix);

					float scale = newDist / oldDist;
					matrix.postScale(scale, scale, mid.x, mid.y);

					float[] values = new float[9];
					matrix.getValues(values);

					Rect localrect = new Rect();
					scaleDragableImageView.getGlobalVisibleRect(localrect);
					
					Rect imageState = new Rect();
					imageState.left = (int)values[2] +localrect.left;
					imageState.top = (int)values[5] +localrect.top;
					imageState.right = (int)(imageState.left + rect.width()* values[0]);
					imageState.bottom = (int)(imageState.top + rect.height()* values[0]);
		
					float scaleLimit = ImageScaleUtil.imageZoomScaleLimit(rect, imageState, localrect);
					if(scaleLimit!=1){
						matrix.postScale(scaleLimit, scaleLimit, mid.x, mid.y);
					}
					imageState = null;
				}
			}
			break;
			
		case MotionEvent.ACTION_UP:
			
			float[] values = new float[9];
			matrix.set(scaleDragableImageView.getImageMatrix());
			matrix.getValues(values);
			
			if(initvalues[0]>=values[0])
			{
				matrix.set(initMatrix);
			}
			
			break;
		}
		scaleDragableImageView.setImageMatrix(matrix);
		return false;
	}
}
